package it.tcm.TeacherClassMate.facade;

import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import it.tcm.TeacherClassMate.beans.TemplateBean;
import it.tcm.TeacherClassMate.dao.TemplateDao;
import it.tcm.TeacherClassMate.persistent.HibernateUtil;


public class TemplateManageBase {

	 	private Logger logger = Logger.getLogger(this.getClass().getName());   
	    private TemplateDao tDao = new TemplateDao();
	  
	    public TemplateBean getTemplateById(Integer id) {
		    Session session = null;
		    TemplateBean template = null;
			try {
				session = HibernateUtil.createNewSessionAndTransaction();
				tDao.setSession(session);
				template = tDao.findById(id, false);
			    HibernateUtil.commitTransactionAndCloseSession(session);
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    logger.error("Error in reading template "  + id.toString(), e);
			} finally {
			}
			return template;
	    }
	    
       
	    public List<TemplateBean> getAll() {
		    Session session = null;
		    List<TemplateBean> eList = null;
			try {
				session = HibernateUtil.createNewSessionAndTransaction();
				tDao.setSession(session);
				eList = tDao.findAll();
			    HibernateUtil.commitTransactionAndCloseSession(session);
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    logger.error("Errore in reading all template ", e);
			} finally {
			}
			return eList;
	    }
	   
	   
}
